import Vue from "vue"
import VueRouter from "vue-router"
Vue.use(VueRouter)

import Login from "@/views/Login.vue"
import Layout from "@/layout"

// 引入路由模块
import recordRouter from "./modules/record"
import educateRouter from "./modules/educate"
import pregnantWomanRouter from "./modules/pregnant-woman"
import childHealthRouter from "./modules/child-health"
import agedRouter from "./modules/aged"
import chronicDisease from "./modules/chronic-disease"
import diseaseControl from "./modules/disease-control"
import criticalRouters from "./modules/critical-illness"
import healthServices from "./modules/health-services"
import setupRouter from "./modules/setup"
import previewRouter from "./modules/preview"
export const constantRoutes = [
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "/redirect/:path*",
        component: () => import("@/views/redirect/index")
      }
    ]
  },
  {
    path: "/Login",
    component: Login,
    hidden: true
  },
  {
    path: "/",
    component: Layout,
    redirect: "/welcome",
    children: [
      {
        path: "welcome", // 首页
        component: () => import("@/views/welcome/"),
        name: "Welcome",
        alwaysShow: true,
        meta: {
          title: "首页",
          icon: "home",
          affix: true
        }
      }
    ]
  },
  ...previewRouter,
  // recordRouter,
  // educateRouter,
  // pregnantWomanRouter,
  // childHealthRouter,
  // agedRouter,
  // chronicDisease,
  // diseaseControl,
  // criticalRouters,
  // healthServices,
  // setupRouter,
  {
    path: "/404",
    component: () => import("@/views/error-page/404"),
    hidden: true
  }
]
export const asyncRoutes = [
  recordRouter,
  educateRouter,
  pregnantWomanRouter,
  childHealthRouter,
  agedRouter,
  chronicDisease,
  diseaseControl,
  criticalRouters,
  healthServices,
  setupRouter,
  { path: "*", redirect: "/404" }
]

// 解决第一次登录 catch给捕获到
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

const createRouter = () =>
  new VueRouter({
    scrollBehavior: () => ({
      y: 0
    }),
    routes: constantRoutes
  })

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
